//
//  OrderDishesViewController.m
//  OrderSeat
//
//  Created by 王威 on 14-1-27.
//  Copyright (c) 2014年 wangwei. All rights reserved.
//

#import "OrderDishesViewController.h"
#import "EditOrderDishViewController.h"
#import "OrderDishesCell.h"

@interface OrderDishesViewController ()

@end

@implementation OrderDishesViewController

- (id)initWithDishId:(NSString *)dishId
{
    self = [super initWithNibClass:[OrderDishesViewController class]];
    if (self) {
        _dishId = dishId;
        _dataSource = [[OrderDishesDataSource alloc] initWithVC:self];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setupUI];
    
    [_dataSource fetchDishes];
}

- (void)setupUI
{
    [self createLeftButton];
    [self setNavigationTitle:@"已点的菜品"];
    
    _dishTable.hidden = YES;
    _dishTable.delegate = _dataSource;
    _dishTable.dataSource = _dataSource;
    [_dishTable drawBorder];
}

- (void)rightButtonClick:(id)sender
{
    EditOrderDishViewController *orderDishVC = [[EditOrderDishViewController alloc] initWithShopId:_dataSource.shopId];
    orderDishVC.orderId = _dishId;
    orderDishVC.dishCatId = _dataSource.catId;
    orderDishVC.orderDishList = _dataSource.updateDishList;
    orderDishVC.orderDishCatList = _dataSource.catList;

    [self pushViewController:orderDishVC];
}

- (void)loadDataFinished
{
    if (_orderType == NewOrder && _dishType == Dish) {
        [self createRightButton:@"修改"];
    }
    
    _dishTable.hidden = NO;
    [_dishTable reloadData];
    if (_dishTable.contentHeight >= ContentNoTabBarHeight-10) {
        _dishTable.height = ContentNoTabBarHeight-15;
        _dishTable.scrollEnabled = YES;
    }else {
        [_dishTable autoHeight];
        _dishTable.scrollEnabled = NO;
    }
}

@end

//==========================================================================================
#pragma ===================数据源类实现=======================================================
//==========================================================================================

#define MYORDER_DISH_TAG 1001
#define MYORDER_UPDATE_TAG 1002

@implementation OrderDishesDataSource

- (id)initWithVC:(id)Controller
{
    self = [super init];
    if (self) {
        self.Controller = Controller;
    }
    
    return self;
}

- (void)fetchDishes
{
    [SVProgressHUD showWithStatus:@"数据正在加载中..."];
    NSString *url = [NSString stringWithFormat:API_MYORDER_DISHES,_Controller.dishId];
    [self get:url tag:MYORDER_DISH_TAG];
}

- (void)fetchUpdateDish
{
    
    NSString *url = [NSString stringWithFormat:API_MYORDER_UPDATE,_Controller.dishId];
    [self get:url tag:MYORDER_UPDATE_TAG];
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
    NSLog(@"%@", request.responseString);
    NSDictionary *json = [request.responseData JSONValue];
    if ([[json objectForKey:@"error_code"] intValue] != 0) {
        return;
    }
    
    NSDictionary *data = [json objectForKey:@"data"];
    if (request.tag == MYORDER_DISH_TAG) {
        self.data = data;
        self.dishList = [data objectForKey:@"caipin"];
        _Controller.dishType = [[data objectForKey:@"type"] intValue];
        
        if (_Controller.dishType == Package) {
            [SVProgressHUD dismiss];
            [_Controller loadDataFinished];
        }else {
            [self fetchUpdateDish];
        }
    }
    
    if (request.tag == MYORDER_UPDATE_TAG) {
        self.updateDishList = [data objectForKey:@"caipin"];
        self.catList = [data objectForKey:@"dishlb"];
        self.shopId = [data objectForKey:@"did"];
        self.catId = [data objectForKey:@"dish_type"];
        
        [SVProgressHUD dismiss];
        [_Controller loadDataFinished];
    }
}

#pragma ===================加载tableview数据 Begin================================================

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dishList.count+1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    int num = indexPath.row == _dishList.count ? 1 : 0;
    NSString *CellId = [NSString stringWithFormat:@"OrderDishesCell_%d",num];
    
    OrderDishesCell *cell = [tableView dequeueReusableCellWithIdentifier:CellId];
    if (cell == nil) {
        NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"OrderDishesCell" owner:self options:nil];
        cell = [array objectAtIndex:num];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    }
    
    NSDictionary *dic = num == 0 ? _dishList[indexPath.row] : _data;
    [cell setContent:dic];
    
    return cell;
}

#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

@end




